package cn.tedu.springmvc.controller;

import cn.tedu.springmvc.dto.UserRegDTO;
import cn.tedu.springmvc.util.JsonResult;
import cn.tedu.springmvc.vo.UserVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import static cn.tedu.springmvc.util.JsonResult.State.ERR_PASSWORD;
import static cn.tedu.springmvc.util.JsonResult.State.ERR_USERNAME;


@RestController
//@Controller //必须是Controller,不可以是其他注解
@RequestMapping("/user")
public class UserController {
    public UserController() {
        System.out.println("UserController.UserController()");
    }
    //@RequestMapping注解主要作用是配置请求路径与处理请求的的映射关系
    @RequestMapping(value = "/{userid:[0-9]+}/info.do" ,method = {RequestMethod.GET,RequestMethod.POST})
    //@ResponseBody//将返回值响应到客户端(响应正文)
   public JsonResult<UserVo> info(@PathVariable ("userid") Long id){
        System.out.println("id = " + id);
        UserVo userVo=new UserVo();
        userVo.setUsername("yueban");
        userVo.setPassword("123456");
        userVo.setEmail("287885002@qq.com");
        return JsonResult.ok(userVo);
    }

    @GetMapping("/login.do")
    public JsonResult<Void> login(String username,String password){
        System.out.println("username = " + username + ", password = " + password);
        if(username.equals("admin")){
            if(password.equals("888888")){
                //登录成功
                return JsonResult.ok();
            }else{
                //密码错误
                String message="登陆失败,密码错误";
                return JsonResult.fail(ERR_PASSWORD,message);
            }

        }else{
            //用户名不存在
            String message="用户名不存在";
            return JsonResult.fail(ERR_USERNAME,message);
        }

    }
    @RequestMapping("/reg.do")
    //用在参数上的注解@RequestParam(required = false,defaultValue = "月半")
    public JsonResult<Void> reg(UserRegDTO userRegDTO){
        System.out.println(userRegDTO );
        return JsonResult.ok();
    }
    @GetMapping(value = "/{username:[a-zA-Z]+}/info.do" )
    //@ResponseBody//将返回值响应到客户端
    public UserVo info(@PathVariable  String username){
        System.out.println("username = " + username);
        UserVo userVo=new UserVo();
        userVo.setUsername("yueban");
        userVo.setPassword("123456");
        userVo.setEmail("287885002@qq.com");
        return userVo;
    }


    @GetMapping("/npe.do")
    public String npe(String username){

            username.toString();
        return "hahaha";
    }

}
